-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
45 lines (38 loc) · 1.33 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.Scanner;
public class IntersectionOfSortedArrays {
public static void findIntersection(int[] arr1, int[] arr2) {
int i = 0, j = 0;
System.out.print("Intersection of the two arrays: ");
while (i < arr1.length && j < arr2.length) {
if (arr1[i] < arr2[j]) {
i++;
} else if (arr1[i] > arr2[j]) {
j++;
} else {
System.out.print(arr1[i] + " ");
i++;
j++;
}
}
System.out.println();
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the first array: ");
int size1 = scanner.nextInt();
int[] arr1 = new int[size1];
System.out.println("Enter the elements of the first sorted array:");
for (int i = 0; i < size1; i++) {
arr1[i] = scanner.nextInt();
}
System.out.print("Enter the size of the second array: ");
int size2 = scanner.nextInt();
int[] arr2 = new int[size2];
System.out.println("Enter the elements of the second sorted array:");
for (int i = 0; i < size2; i++) {
arr2[i] = scanner.nextInt();
}
findIntersection(arr1, arr2);
scanner.close();
}
}